BuildingID = {ZhiHuiBu=1001, JunXiao=1002, KeXueYuan=1003, ZhanFuYing=1004, XunLianYing=1005, YinHang=1006, JunYing=1007, JinJiChang=1008}
BuildingLevel  = {[1001]="BaseLevel",[1002]="BaseLevel",[1003]="MilitaryLevel",[1004]="CampPowLevel",[1005]="SchoolLevel",[1006]="BankLevel",[1007]="CampLevel"}

function formatSecond(sec)
  local h = math.floor(sec/3600)
  local m = math.floor((sec-h*3600)/60)
  local s = math.floor(sec-h*3600-m*60)
  --if h~=0 then
  --  return string.format("%02d:%02d:%02d", h, m, s)
  --end
  --return string.format("%02d:%02d", m, s)
  return string.format("%02d:%02d:%02d", h, m, s)
end

function split(data, flag)
  if data==nil then return nil;end
  pos=0
  local arr = Array:create()
  while 1 do
    local i, j = string.find(data, flag, pos)
    if i==nil or pos>5000 then break end
    local tmp = string.sub(data, pos, i-1)
    pos = j+1
    arr:append(tmp)
  end
  arr:append(string.sub(data, pos))
  return arr
end
function checkPasswd(data)
  for i=1,string.len(data) do  
    if string.byte(data, i)==string.byte(" ") then
      return false
    end
  end
  return true
end
function checkUserAccount(data)
  for i=1,string.len(data) do  
    if ((string.byte(data, i)>=string.byte("0") and string.byte(data, i)<=string.byte("9")) 
        or (string.byte(data, i)>=string.byte("A") and string.byte(data, i)<=string.byte("Z"))
        or (string.byte(data, i)>=string.byte("a") and string.byte(data, i)<=string.byte("z"))
        or string.byte(data, i)==string.byte("@") or string.byte(data, i)==string.byte("_")) then
    else
      return false
    end
  end
  return true
end
--print(checkUserAccount("fda@13$AD_fda"))
function stringLimit(str,len)
  local res=""
  local count=0
  local i=1
  while i<=string.len(str) do
    local pos=i
    if string.byte(str, i, i) > 127 then
      pos = i+2
    end
    res=string.format("%s%s", res, string.sub(str, i, pos))
    i = pos+1
    if i-count>len then
      res=string.format("%s...", res)
      count = i
      break
    end
  end
  return res
end

function autoEnter(str,len)
  local res=""
  local count=0
  local i=1
  while i<=string.len(str) do
    local pos=i
    if string.byte(str, i, i) > 127 then
      pos = i+2
    end
    res=string.format("%s%s", res, string.sub(str, i, pos))
    i = pos+1
    if i-count>len then
      res=string.format("%s\n", res)
      count = i
    end
  end
  return res
end

function tableSize(t)
  local c=0
  for k,v in pairs(t) do
    c = c+1
  end
  return c
end
function showMessage(msg, color)
  --local label = CCLabelTTF:create(string.format("兵力增加%d", count), "Arial", 32)
  local label = CCLabelTTF:create(msg, "Arial", 32)
  label:setColor(ccc3(0,255,0))
  if color~=nil then
    label:setColor(color)
  end
  label:setPosition(ccp(480, 320-100))
  local sc = CCDirector:sharedDirector():getRunningScene()
  sc:addChild(label, 112)

  local mac = CCMoveBy:create(0.3, ccp(0, 150))
  local sac = CCScaleBy:create(0.3, 1.5)
  label:runAction(mac)
  label:runAction(ActionUtil.createSeqAction({sac, CCDelayTime:create(1), CCFadeOut:create(0.2), CCCallFuncN:create(onRemoveFromParent)}))
end


local BuildingItemID = {TitleLabel=1001, TimeRequireLabel=1002, CashRequireLabel=1003, ExploitRequireLabel=1004, LevelRequireLabel=1007}
function setBuildingBaseData(bid, node)
  local zhihuibuLevel = UserData.userinfo.host.cities2[tostring(BuildingID.ZhiHuiBu)].l
  local buildingLevel = UserData.userinfo.host.cities2[tostring(bid)].l
  local buildingInfo = UserData.config.models[BuildingLevel[bid]][buildingLevel]
  local buildingName = UserData.config.models.Building2[bid-1000].name
  local lb = node:getChildByTag(BuildingItemID.TitleLabel)
  TypeCast(lb, "CCLabelTTF"):setString(string.format("%s%d级", buildingName, buildingLevel))
  
  lb = node:getChildByTag(BuildingItemID.LevelRequireLabel)
  local rl = buildingLevel+1
  if bid==BuildingID.ZhiHuiBu then
    if rl<=10 then rl=1 end
    TypeCast(lb, "CCLabelTTF"):setString(string.format("角色等级%d", rl))
    if Player.level < rl then
      TypeCast(lb, "CCLabelTTF"):setColor(ccc3(255,0,0))
      TypeCast(node:getChildByTag(1020), "CCButton"):setEnabled(false)
    end
  else
    TypeCast(lb, "CCLabelTTF"):setString(string.format("指挥部等级%d", rl))
    if zhihuibuLevel < rl then
      TypeCast(lb, "CCLabelTTF"):setColor(ccc3(255,0,0))
      TypeCast(node:getChildByTag(1020), "CCButton"):setEnabled(false)
    end
  end
  lb = node:getChildByTag(BuildingItemID.TimeRequireLabel)
  TypeCast(lb, "CCLabelTTF"):setString(formatSecond(buildingInfo.coolingTime))
  lb = node:getChildByTag(BuildingItemID.CashRequireLabel)
  TypeCast(lb, "CCLabelTTF"):setString(tostring(buildingInfo.cash))
  lb = node:getChildByTag(BuildingItemID.ExploitRequireLabel)
  TypeCast(lb, "CCLabelTTF"):setString(tostring(buildingInfo.exploit))
end


function nameFromPath(path)
  local p1,p2 = string.find(string.reverse(path), "/")
  return string.sub(path, -p1+1)
end

function addSpriteFromBK(node, bkid, imgName)
  local imgBK = node:getChildByTag(bkid)
  local sp = CCSprite:create(imgName)
  sp:setPosition(imgBK:getPosition())
  node:addChild(sp)
  return sp
end
function addLayerFromBK(node, bkid, layer)
  local imgBK = node:getChildByTag(bkid)
  layer:setPosition(imgBK:getPosition())
  node:addChild(layer)
end
function addLabelFromBK(node, bkid, text, font, fontsize, offset)
  if offset==nil then offset=ccp(0,0) end
  local imgBK = node:getChildByTag(bkid)
  local lb = CCLabelTTF:create(text, font, fontsize)
  local x,y = imgBK:getPosition()
  lb:setPosition(ccp(x+offset.x, y+offset.y))
  node:addChild(lb)
  return lb 
end
function addButtonFromBK(node, bkid, imgName, title, action, target)
  local b = CCButton:createWithFileName(action, imgName, imgName, imgName, title)
  setObjectExtTable(b, target)
  local imgBK = node:getChildByTag(bkid)
  --print("addButtonFromBK", bkid, imgBK)
  b:setPosition(imgBK:getPosition())
  node:addChild(b)
  return b 
end
function addLabel(parent, pos, text, font, fontsize)
  local lb = CCLabelTTF:create(text, font, fontsize)
  lb:setPosition(pos)
  parent:addChild(lb)
  return lb 
end
function addSprite(parent, pos, imgName)
  local sp = CCSprite:create(imgName)
  sp:setPosition(pos)
  parent:addChild(sp)
  return sp
end
function setSpriteImg(sp, imgName)
  local newSp = CCSprite:create(imgName)
  newSp:setPosition(sp:getPosition())
  sp:getParent():addChild(newSp)
  sp:getParent():removeChild(sp, true)
  return newSp
end
function createItemLayer(item)
  local layer = CCLayer:create()
  local sp = nil
  if item.type=="cash" then
    sp = CCSprite:create("cash2.png")
  elseif item.type=="xp" then
    sp = CCSprite:create("exp2.png")
  elseif item.type=="coin" then
    sp = CCSprite:create("coin2.png")
  elseif item.type=="exploit" then
    sp = CCSprite:create("exploit2.png")
  elseif item.type=="soldiers" then
    sp = CCSprite:create("soldier2.png")
  elseif item.type=="reputation" then
    sp = CCSprite:create("cash2")
  elseif item.type=="item" then
    local tmp = Item:createWithId(item.id)
    local imgName = nameFromPath(tmp.itemConfig.asset)
    sp = CCSprite:create(imgName)
  elseif item.type=="equip" then
    local tmp = UserData.config.models.Equip[item.id]
    --local tmp = Equip:createWithId(item.id)
    local imgName = nameFromPath(tmp.asset)
    sp = CCSprite:create(imgName)
  elseif item.type=="decorate" then
    local tmp = Equip:createWithId(item.id)
    local imgName = nameFromPath(tmp.itemConfig.asset)
    sp = CCSprite:create(imgName)
  end
  print("createItemLayer", item.type, sp)
  layer:addChild(sp)
  addLabel(layer, ccp(0, -20), tostring(item.num), "Helvetica", 28)
  return layer  
end
